package top.broncho.lottery.ui.history

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import org.jetbrains.anko.info
import top.broncho.lottery.R
import top.broncho.lottery.data.paging.Loading
import top.broncho.lottery.databinding.ActivityHistoryBinding
import top.broncho.lottery.ui.base.BaseDiActivity
import top.broncho.lottery.ui.widget.SimpleListDividerDecorator

class HistoryActivity : BaseDiActivity<HistoryViewModule, ActivityHistoryBinding>() {
    override fun getLayoutId() = R.layout.activity_history

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding?.vm = viewModule
        binding?.lifecycleOwner = this

        val adapter = HistoryAdapter { viewModule.retry() }
        binding?.recyclerView?.adapter = adapter
        binding?.recyclerView?.addItemDecoration(
            SimpleListDividerDecorator(
                ContextCompat
                    .getDrawable(
                        applicationContext,
                        R.drawable.list_divider_h
                    ), null, false
            )
        )
        binding?.vm?.historyList?.observe(this, Observer { adapter.submitList(it) })
        binding?.vm?.loadingState?.observe(this, Observer {
            info { "refreshState: isInit = ${it.isInit}, $it" }
            if (it.isInit) {
                //更新下拉刷新的状态
                binding?.swipeRefreshLayout?.post {
                    binding?.swipeRefreshLayout?.isRefreshing = it is Loading
                }
            }
            binding?.recyclerView?.post { adapter.setLoadingState(it) }
        })

    }

}
